<?php

/*
 * File  : produk.php
 * Table : produks, kategoris
 */
//session_start();
//cek session

if ((!$_SESSION["USRID"] AND !$_SESSION["USRPASS"])) {
    header("location: ../index.php");
    exit();
}
//end
?>

<?php

$aksi = "cms/bab/m_produks/aksi_produk.php";
switch ($_GET["opr"]) {

// tampil produk
    default:
        echo "<h2>Produk</h2>";
        echo " <input type=button value='Tambah Produk' onclick=\"window.location.href='?module=produk&opr=tambah';\">";
        echo " <table>";
        echo "  <tr><th>No</th><th>Nama Produk</th><th>Kategori</th><th>Berat(gram)</th><th>Harga [IDR]</th><th>Diskon</th><th>Stok</th><th>Tgl Masuk</th><th>Aksi</th></tr>";

        $p = new Paging;
        $batas = 10;
        $posisi = $p->cariPosisi($batas);

        $tampil = mysql_query("SELECT * FROM produks where produk_id<>13 ORDER BY produk_id DESC LIMIT $posisi, $batas");

        $no = $posisi + 1;
        while ($row = mysql_fetch_array($tampil)) {
            $tanggal = tgl_indo($row["produk_tgl_masuk"]);
            $harga = format_rupiah($row["produk_harga"]);
            echo "<tr><td>$no</td>";
            echo " <td>" . $row["produk_nama"] . "</td>";
            echo " <td>" . get_kategori_nama_from_id($row["produk_kategori_id"]) . "</td>";
            echo " <td align=center>" . $row["produk_berat"] . "</td>";
            echo " <td align=right>$harga</td>";
            echo " <td align=center>" . $row["produk_diskon"] . "</td>";
            echo " <td align=center>" . $row["produk_stok"] . "</td>";
            echo " <td>$tanggal</td>";
            echo " <td><a href='?module=produk&opr=edit&id=" . $row["produk_id"] . "'>Edit</a> | 
		                <a href='" . URLHOST . "$aksi?module=produk&opr=hapus&id=" . $row["produk_id"] . "'  onClick=\"return confirm('Hapus Produk " . $row["produk_nama"] . "?')\" >Hapus</a>";
            echo " </td></tr>";
            $no++;
        }
        echo "</table>";

        $jmldata = mysql_num_rows(mysql_query("SELECT * FROM produks"));
        $jmlhalaman = $p->jumlahHalaman($jmldata, $batas);
        $linkHalaman = $p->navHalaman($_GET["halaman"], $jmlhalaman);

        echo "<div id=paging>Hal: $linkHalaman</div><br>";
        break;

// tambah produk
    case "tambah":
        load_tinymce();
        echo "<h2>Tambah Produk</h2>";
        echo " <form method='POST' action='" . URLHOST . "$aksi?module=produk&opr=input' enctype='multipart/form-data'>";
        echo " <table>";
        echo "  <tr><td width=70>Nama Produk</td><td> : <input type='text' name='in_produk_nama' size='60'></td></tr>";
        echo "  <tr><td>Kategori</td>  <td> :";
        echo "   <select name='in_produk_kategori_id'>";
        echo "    <option value='0' selected>- Pilih Kategori -</option>";
        $tampil = mysql_query("SELECT * FROM kategoris ORDER BY kategori_nama");
        while ($row = mysql_fetch_array($tampil)) {
            echo "<option value='" . $row["kategori_id"] . "'>" . $row["kategori_nama"] . "</option>";
        }
        echo "   </select></td></tr>";
        echo "  <tr><td>Berat</td><td> : <input type ='text' name = 'in_produk_berat' size ='3'></td></tr>";
        echo "  <tr><td>Harga</td><td> : <input type = 'text' name = 'in_produk_harga' size ='10'></td></tr>";
        echo "  <tr><td>Diskon</td><td> : <input type = 'text' name = 'in_produk_diskon' value='0' size ='10'></td></tr>";
        echo "  <tr><td>Stok</td><td> : <input type = 'text' name = 'in_produk_stok' size = '3'></td></tr>";
        echo "  <tr><td>Deskripsi</td><td> <textarea name = 'in_produk_deskripsi' style = 'width: 600px; height: 350px;'></textarea></td></tr>";
        echo "  <tr><td>Gambar</td><td> : <input type = file name = 'in_produk_gambar' size = 40>";
        echo "  <br>Tipe gambar harus JPG/JPEG dan ukuran lebar maks: 400 px</td></tr>";
        echo "  <tr><td colspan='2'><input type ='submit' value ='Simpan'>";
        echo "  <input type ='button' value ='Batal' onclick ='self.history.back()'></td></tr>";
        echo " </table>";
        echo " </form>";
        break;

    case "edit":
        $edit = mysql_query("SELECT * FROM produks WHERE produk_id = '" . $_GET["id"] . "'");
        $row = mysql_fetch_array($edit);

        load_tinymce();
        echo "<h2>Edit Produk</h2>";
        echo " <form method='POST' action ='" . URLHOST . "$aksi?module=produk&opr=update' enctype='multipart/form-data'>";
        echo " <input type ='hidden' name ='id' value ='" . $row["produk_id"] . "'>";
        echo " <table>";
        echo "  <tr><td width = 70>Nama Produk</td> <td> : <input type='text' name = 'in_produk_nama' size ='60' value = '" . $row["produk_nama"] . "'></td></tr>";
        echo "  <tr><td>Kategori</td> <td> : <select name = 'in_produk_kategori_id'>";

        $tampil = mysql_query("SELECT * FROM kategoris ORDER BY kategori_nama");
        if ($row["produk_kategori_id"] == 0) {
            echo "<option value = 0 selected>- Pilih Kategori -</option>";
        }

        while ($w = mysql_fetch_array($tampil)) {
            if ($row["produk_kategori_id"] == $w["kategori_id"]) {
                echo "<option value ='" . $w["kategori_id"] . "' selected>" . $w["kategori_nama"] . "</option>";
            } else {
                echo "<option value ='" . $w["kategori_id"] . "'>" . $w["kategori_nama"] . "</option>";
            }
        }
        echo "  </select></td></tr>";
        echo "  <tr><td>Berat</td> <td> : <input type ='text' name = 'in_produk_berat' value ='" . $row["produk_berat"] . "' size ='3'></td></tr>";
        echo "  <tr><td>Harga</td> <td> : <input type ='text' name = 'in_produk_harga' value ='" . $row["produk_harga"] . "' size ='10'></td></tr>";
        echo "  <tr><td>Diskon</td><td> : <input type = 'text' name = 'in_produk_diskon' value ='" . $row["produk_diskon"] . "' size ='10'></td></tr>";
        echo "  <tr><td>Stok</td> <td> : <input type = text name = 'in_produk_stok' value ='" . $row["produk_stok"] . "' size = 3></td></tr>";
        echo "  <tr><td>Deskripsi</td> <td> <textarea name = 'in_produk_deskripsi' style = 'width: 600px; height: 350px;'>" . $row["produk_deskripsi"] . "</textarea></td></tr>";
        echo "  <tr><td>Gambar</td> <td> : <img src = '../media/foto_produk/small_" . $row["produk_gambar"] . "'></td></tr>";
        echo "  <tr><td>Ganti Gbr</td> <td> : <input type ='file' name = 'in_produk_gambar' size = '30'> *)</td></tr>";
        echo "  <tr><td colspan = 2>*) Apabila gambar tidak diubah, dikosongkan saja.</td></tr>";
        echo "  <tr><td colspan = 2><input type ='submit' value ='Update'><input type ='button' value ='Batal' onclick = self.history.back()></td></tr>";
        echo "  </table></form>";
        break;
}
?>
